<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"   lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title>组织机构管理</title>
        <link rel="stylesheet" type="text/css" href="/js/jquery-easyui-1.5.4.5/themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="/js/jquery-easyui-1.5.4.5/themes/icon.css">
        <script src="/js/jquery-easyui-1.5.4.5/jquery.min.js"></script>
        <script src="/js/jquery-easyui-1.5.4.5/jquery.easyui.min.js"></script>
    </head>

    <body>
	    <table class="easyui-treegrid" title="组织机构管理" style="width:98%;height:500px" id="table-grid"
			data-options="idField:'departId',treeField:'departName',singleSelect:true,collapsible:false,
			url:'/department/list',method:'get',toolbar:toolbar,pagination:true,pageSize:20">
		<thead>
			<tr>
				<th data-options="field:'departId',width:200">机构编号</th>
				<th data-options="field:'departName',width:400">机构名称</th>
				<th data-options="field:'departStatus',width:100,formatter: function(value,row,index){
				if(value == '0') return '无效';
				if(value == '1') return '有效';
			}">机构状态</th>
			</tr>
		</thead>
	</table>
	<div id="saveWindows" class="easyui-window" title="Modal Window" data-options="modal:true,closed:true,iconCls:'icon-save'" style="width:500px;height:300px;padding:10px;">
		<form id="ff" class="easyui-form" method="post" data-options="novalidate:true">
			<table>
				<tr>
					<td width="80px">机构编号:</td>
	    			<td>
	    				<input class="easyui-textbox" type="text" name="departId" id="departId" data-options="required:true" width='300px'></input>
	    			</td>
				</tr>
				<tr>
					<td>机构名称:</td>
	    			<td>
	    				<input class="easyui-textbox" type="text" name="departName" id="departName" data-options="required:true" width="300px"></input>
	    			</td>
				</tr>
				<tr>
					<td>机构状态:</td>
	    			<td>		    			
						<select id="departStatus" class="easyui-combobox" name="departStatus" id="departStatus" style="width:200px;">
						    <option value="1">有效</option>
						    <option value="0">无效</option>
						</select>
		    			<input  type="hidden" name="parentId" id="parentId"></input>
						<input id='insertMark' name='insertMark' type='hidden' value='0'>
					</td>
				</tr>
				<tr>
				<td><button>保存</button></td>
				<td><input type='button' onclick="$('#saveWindows').window('close');" value='取消'/></td>
				</tr>
			</table>
		</form>
	</div>
	
  <script type="text/javascript">		
	//dataGrid工具栏
	var toolbar = [{
				text:'新增',
				iconCls:'icon-add',
				handler:function(){
					var tt = $('#table-grid');
					var node = tt.treegrid('getSelected');
					var pId = null;
					if(node){
						pId = node.departId;
					}
					$('#insertMark').val('1');
					$('#parentId').val(pId);
					$("#saveWindows").window('open');
				}
			},{
				text:'编辑',
				iconCls:'icon-edit',
				handler:function(){
					var tt = $('#table-grid');
					var row=tt.treegrid('getSelected');
					if(row){
						var rowId =row.departId;
						$('#insertMark').val('0');
						if(row._parentId)
							$('#parentId').val(row._parentId);
						else
							$('#parentId').val("");
						$('#departId').textbox("setValue",row.departId);
						$('#departName').textbox("setValue",row.departName);
						$('#departStatus').combobox("select",row.departStatus);
						
						$("#saveWindows").window('open');
					}else{
						$.messager.alert('Warning','请选择要编辑的行！');
					}
				}
			},{
				text:'删除',
				iconCls:'icon-remove',
				handler:function(){
							var tt = $('#table-grid');
							var row=tt.treegrid('getSelected');
							if(!row){
								$.messager.alert('Warning','请选择要删除的行！');
							}
					$.messager.confirm('提示','您确定要删除吗？',function (r){
						if(r){
							$.ajax({
								   type: "POST",
								   dataType:"json",
								   url: "/department/del",
								   data: {departId:row.departId},
								   success: function(msg){
								     if(msg){
								    	 $.messager.alert('Warning',msg.retMsg);
								    	 $('#table-grid').treegrid('reload');
								     }
								   }
								});
						}
					});
				}
			}];
	$(function(){
		$('#ff').form({
			url:"/department/save",
			onSubmit: function(){
				var isValid = $(this).form('validate');
				if (!isValid){
					$.messager.alert('Warning','请填写必填项');
				}
				return isValid;	
			},
			success:function(msg){
			     if(msg){
			    	 var data = eval('(' + msg + ')');
			    	 $.messager.alert('Warning',data.retMsg);
			    	 $('#table-grid').treegrid('reload');
			    	 $("#saveWindows").window('close');
			     }
			}
		});
	});
	</script>      
    </body>
</html>
